package br.com.lavitstudio.game;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Mob extends DoubleRectangle {
	public int[] id;
	
	public boolean isJumping = false;
	public boolean isMoving = false;
	public boolean isFalling = false;
	
	public double movementSpeed = 0.4;
	public double falliingSpeed = 1;
	public double dir = movementSpeed;
	public double jumpingSpeed = 1.2;
	
	public int jumpingHeight = 30;
	public int jumpingCount = 0;
	public int animation = 0;
	public int animationFrame = 0;
	public int animationTime = 10;
	public int animationFrameMax = 2;
	
	public int doubleHeight = 1;
	public int revisionY = 0;
	
	public BufferedImage sprites;
	
	public Mob(int x, int y, int width, int height, int[] id, BufferedImage sprites, int doubleHeight) {
		setBounds(x, y, width, height);
		
		this.id = id;
		this.sprites = sprites;
		this.doubleHeight = doubleHeight;
	}

	public Mob(int x, int y, int width, int height, int[] id, BufferedImage sprites, int doubleHeight, int revisionY) {
		setBounds(x, y, width, height);
		
		this.id = id;
		this.sprites = sprites;
		this.doubleHeight = doubleHeight;
		this.revisionY = revisionY;
	}
	
	public void tick() {
		if(!isJumping && !CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y,new Point((int) (x + 2), (int) (y + height)), new Point((int) (x + width - 2), (int) (y + height)))) { // Normal jump
			y += falliingSpeed;
			isFalling = true;
			
		} else {
			isFalling = false;
			
			if(new Random().nextInt(100) < 1) {
				isMoving = true;
				
				if(new Random().nextInt(100) < 50) {
					dir = -movementSpeed;
				} else {
					dir = movementSpeed;
				}
			}
		}
		
		// caminhar
		if(isMoving) {
			boolean canMove = false;

			if(dir == movementSpeed) {
				canMove = CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) (x + width), (int) y), new Point((int) (x + width), (int) (y + (height - 2))));
			} else if(dir == -movementSpeed) {
				canMove = CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) x, (int) y), new Point((int) x, (int) (y + (height - 2))));
			}
			
			if(canMove && !isFalling) {
				isJumping = true;
			}
			
			if(animationFrame >= animationTime) {
				if(animation > animationFrameMax) {
					animation = 1;
				}else {
					animation += 1;
				}
				
				animationFrame = 0;
			} else {
				animationFrame += 1;
			}
			
			if(!canMove) {
				x += dir;
			}
		} else {
			animation = 0;
		}
		
		// Pulo
		if(isJumping) {
			if(!CollidingWith.isCollidingWithBlock((int) this.x, (int) this.y, new Point((int) (x + 2), (int) y), new Point((int) (x + width - 2), (int) y))) {
				if(jumpingCount >= jumpingHeight){
					isJumping = false;
					jumpingCount = 0;
				} else {
					y -= jumpingSpeed;
										
					jumpingCount += 1;
				}
			} else {
				isJumping = false;
				jumpingCount = 0;
			}
		}
	}
	
	public static void killAllMobsDay(){
		Component.mob.clear();
	}

	public static void killAllMobsNight(){
		Component.mobAnimal.clear();
	}
	
	public void render(Graphics g) {
		if(dir == movementSpeed) {
			g.drawImage(sprites, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (id[0] * Tile.tileSize) + (Tile.tileSize * animation), id[1] * Tile.tileSize, (id[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, id[1] * Tile.tileSize + (int) height, null);
		} else {
			g.drawImage(sprites, (int) (x - Component.sX), (int) (y - Component.sY) + revisionY, (int) (x + width - Component.sX), (int) (y + height - Component.sY) + revisionY, /**/ (id[0] * Tile.tileSize) + (Tile.tileSize * animation) + (int) width, id[1] * Tile.tileSize, (id[0] * Tile.tileSize) + (Tile.tileSize * animation), id[1] * Tile.tileSize + (int) height, null);
		}
	}
}
